<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/jsp/commons/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="moil">
<head>
    <%@include file="/WEB-INF/jsp/commons/meta.jsp" %>
    <title>${title}</title>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <!-- 响应式设计 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body ng-controller="AllDocCtrl">
<script>
    (function () {
        angular.module("moil", []);
        window.AllDocCtrl = function ($scope, $rootScope) {

        }
    })();
    $(document).ready(function () {
        //聚焦第一个输入框
        $("#account").focus();
        //为inputForm注册validate函数
        $("#inputForm").validate({
            rules: {
                account: {
                    required: true,
                    remote: {
                        type: "POST",
                        url: "${ctx}/security/user/repeat",
                        data: {
                            account: function () {
                                return $("#account").val();
                            },
                            id: ${user.id == null ? 0:user.id}
                        }
                    }
                },
                name: "required",
                password: {
                    required: true,
                    minlength: 6
                },
                "roles.id": "required"
            },
            messages: {
                account: {
                    remote: $.format("用户名已存在，请重新输入！")
                }
            }
        });
        //密码修改
        $("#password").focus(function () {
            var value = $(this).val();
            if (value != "") {
                $(this).hide();
                $("#password2").show().focus();
            }
        });
        $("#password2").blur(function () {
            var value = $(this).val();
            $(this).hide();
            $("#password").show();
            if (value != "") {
                $("#password").val(value);
            } else {
                $("#password").val($("#password").attr("oldValue"));
            }
        });
    });
</script>
<div class="u_child_box">
    <div class="u_title_bar">
        <span id="device_info" class="u_title">用户编辑</span>
    </div>
</div>
<form:form id="inputForm" commandName="user" cssClass="well form-inline" action="${ctx}/security/user/save"
           method="post">
    <form:hidden path="id"/>
    <input name="oldPwd" type="hidden" value="${user.password }"/>
    <table class="table table-striped table-bordered table-condensed">
        <thead>
        <strong>
            <i class="icon-flag"></i>
            用户表单
        </strong>
        </thead>
        <tr>
            <td class="span2">账号:</td>
            <td>
                <form:input path="account" maxlength="30"/>
                <form:errors path="account"></form:errors>
            </td>
        </tr>
        <c:if test="${user.password != null }">
            <tr>
                <td>密码:</td>
                <td>
                    <input id="password2" class="hide" name="password2" type="text" value="" size="30" maxlength="80"/>
                    <input name="password" id="password" type="password" value="${user.password }"
                           oldValue="${user.password }" maxlength="80"/>
                    <form:errors path="password"></form:errors>
                </td>
            </tr>
        </c:if>
        <c:if test="${user.password == null }">
            <tr>
                <td>密码:</td>
                <td>
                    <form:password path="password" maxlength="80"/>
                </td>
            </tr>
        </c:if>
        <tr>
            <td>所属部门:</td>
            <td>
                <form:select path="department.id" items="${departments}" itemLabel="name" itemValue="id"></form:select>
            </td>
        </tr>

        <tr>
            <td>角色:</td>
            <td>
                <c:forEach items="${rolesList }" var="roles">
                    <label class="checkbox">
                        <input name="roles.id" type="checkbox"
                        <c:forEach items="${user.roles }" var="r">
                        <c:if test="${roles.id == r.id}">
                               checked="checked"
                        </c:if>
                        </c:forEach>
                               value="${roles.id}">
                            ${roles.name }
                    </label>
                </c:forEach>
            </td>
        </tr>
        <tr>
            <td>姓名:</td>
            <td>
                <input id="name" name="name" type="text" value="${user.name}" size="30" maxlength="80"/>
                <form:errors path="name">请输入用户名</form:errors>
            </td>
        </tr>
        <tr>
            <td>电话:</td>
            <td>
                <input id="phone" name="phone" type="text" value="${user.phone}" size="30" maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>邮箱E-mail:</td>
            <td>
                <input id="email" name="email" type="text" value="${user.email}" size="30" maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>禁用:</td>
            <td>
                <label class="radio">
                    <input name="enable" type="radio" <c:if test="${!user.enable}">
                        checked="checked"
                    </c:if> value="0"/>
                    是
                </label>
                <label class="radio">
                    <input name="enable" type="radio" <c:if test="${user.enable}">
                        checked="checked"
                    </c:if> value="1"/>
                    否
                </label>
            </td>
        </tr>
        <tr>
            <td>备注:</td>
            <td>
                <textarea id="memo" name="memo" rows="4" class="span6">${user.memo}</textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input class="btn" type="submit" value="提交"/>
                &nbsp;
                <input class="btn" type="button" value="返回" onclick="history.back()"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>